package com.xcf.controller.business.common;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xcf.controller.base.BaseController;
import com.xcf.service.business.common.domesticGoods.DomesticGoodsService;
import com.xcf.util.PageData;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Controller
@RequestMapping(value="/domesticGoods")
public class DomesticGoodsController extends BaseController{
	@Resource
	private DomesticGoodsService domesticgoodsservice;
	String menuUrl="domesticGoods/src";//菜单地址(权限用) 
	@RequestMapping(value="/src")
	public ModelAndView domesticGoods(){
		ModelAndView mv= new ModelAndView();
		mv.setViewName("/business/common/domesticGoods/domesticGoods_src");
		return mv;
		
	}
	
	@RequestMapping(value="/list")
	public void list(HttpServletResponse response) throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		JSONArray arry= new JSONArray();
		List<PageData> list=domesticgoodsservice.serch(pd);
		int total = list.size();
		   for (PageData data : list) {
			  JSONObject ob = new JSONObject();
			ob.put("ID", data.get("ID"));
			ob.put("SPELL_CODE", data.getString("SPELL_CODE"));
			ob.put("SRC_PLACE_CODE", data.getString("SRC_PLACE_CODE"));
			ob.put("SRC_PLACE_NAME", data.getString("SRC_PLACE_NAME"));
			String enable=null;
			if(data.get("ENABLE")==null||data.get("ENABLE").equals(false)){
				enable="否";
				}
			else{
				enable="是";	
				}
			
			ob.put("ENABLE", enable);
			arry.add(ob);
			
	}
		   response.setContentType("text/html; charset=utf-8");         
		     response.getWriter().write("{"+"\""+"total"+"\""+":"+"\""+total+"\""+","+"\""+"rows"+"\""+":"+arry+"}");
	}
	//保存及修改
	@RequestMapping(value="/save")
	@ResponseBody
	public String save() throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		if(pd.get("ID").toString().isEmpty()){
			domesticgoodsservice.save(pd);
		}else{
			
			domesticgoodsservice.updite(pd);
		}
		return null;
		
	}
	//删除
	@RequestMapping(value="/dele")
	@ResponseBody
	public String dele() throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		String data= pd.getString("array");
		JSONArray dite = JSONArray.fromObject(data);
	for (int i = 0; i < dite.size(); i++) {
		JSONObject ob = dite.getJSONObject(i);
		String ID = ob.getString("ID").trim();
		PageData phq = new PageData();
		phq.put("ID", ID);
		domesticgoodsservice.delte(phq);
	}
	return null;
		
	}
}
